home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / interp / char.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-28  |  3.2 KB  |  144 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: char.c,v 1.6 94/06/27 16:31:29 wlott Exp $
  27. *
  28. * This file implements characters.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include <stdio.h>
  33.  
  34. #include "mindy.h"
  35. #include "obj.h"
  36. #include "gc.h"
  37. #include "class.h"
  38. #include "num.h"
  39. #include "bool.h"
  40. #include "error.h"
  41. #include "print.h"
  42. #include "list.h"
  43. #include "type.h"
  44. #include "def.h"
  45. #include "char.h"
  46.  
  47. obj_t obj_CharacterClass;
  48. static obj_t obj_Characters[256];
  49.  
  50. obj_t int_char(int c)
  51. {
  52.     obj_t res = obj_Characters[c];
  53.  
  54.     if (res == NULL) {
  55.     res = alloc(obj_CharacterClass, sizeof(struct character));
  56.     obj_ptr(struct character *, res)->c = c;
  57.     obj_Characters[c] = res;
  58.     }
  59.  
  60.     return res;
  61. }
  62.  
  63.  
  64. /* Dylan routines. */
  65.  
  66. static obj_t int_as_char(obj_t class, obj_t i)
  67. {
  68.     int c = fixnum_value(i);
  69.  
  70.     if (0 <= c && c < 256)
  71.     return int_char(c);
  72.     else {
  73.     error("Can't make a character out of %=", i);
  74.     return NULL;
  75.     }
  76. }
  77.  
  78. static obj_t char_as_int(obj_t class, obj_t c)
  79. {
  80.     return make_fixnum(char_int(c));
  81. }
  82.  
  83.  
  84. /* Printing stuff. */
  85.  
  86. static void print_char(obj_t obj)
  87. {
  88.     int c = char_int(obj);
  89.  
  90.     if (c < ' ' || c > '~')
  91.     printf("'\\%03o'", c);
  92.     else if (c == '\'')
  93.     printf("'\\''");
  94.     else
  95.     printf("'%c'", c);
  96. }
  97.  
  98.  
  99. /* GC stuff. */
  100.  
  101. static int scav_char(struct object *ptr)
  102. {
  103.     return sizeof(struct character);
  104. }
  105.  
  106. static obj_t trans_char(obj_t c)
  107. {
  108.     return transport(c, sizeof(struct character));
  109. }
  110.  
  111. void scavenge_char_roots(void)
  112. {
  113.     int i;
  114.  
  115.     scavenge(&obj_CharacterClass);
  116.  
  117.     for (i = 0; i < 256; i++)
  118.     if (obj_Characters[i] != NULL)
  119.         scavenge(obj_Characters + i);
  120. }
  121.  
  122.  
  123. /* Init stuff. */
  124.  
  125. void make_char_classes()
  126. {
  127.     obj_CharacterClass = make_builtin_class(scav_char, trans_char);
  128. }
  129.  
  130. void init_char_classes()
  131. {
  132.     init_builtin_class(obj_CharacterClass, "<character>",
  133.                obj_ObjectClass, NULL);
  134.     def_printer(obj_CharacterClass, print_char);
  135. }
  136.  
  137. void init_char_functions()
  138. {
  139.     define_method("as", list2(singleton(obj_CharacterClass), obj_IntegerClass),
  140.           FALSE, obj_False, FALSE, obj_CharacterClass, int_as_char);
  141.     define_method("as", list2(singleton(obj_IntegerClass), obj_CharacterClass),
  142.           FALSE, obj_False, FALSE, obj_IntegerClass, char_as_int);
  143. }
  144.